package com.example.factory;

import com.example.proxy.CglibTransactionInterceptor;
import com.example.proxy.JdkTransactionHandler;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class ProxyFactory {
    public static Object createProxy(Object target) {
        // 判断目标对象是否实现了接口
        if (target.getClass().getInterfaces().length > 0) {
            System.out.println("接口："+target.getClass().getInterfaces());
            return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new JdkTransactionHandler(target)
            );
        } else {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(target.getClass());
            enhancer.setCallback(new CglibTransactionInterceptor());
            return enhancer.create();
        }
    }
}